using UnityEngine;
using System.Collections;

public class EnemyMovement : MonoBehaviour 
{
	public float movementSpeed = 3.0f;
	public float zigZagSpeed = 2.0f;
	public float zigZagDistance = 2.0f;
	
	private float _startYposition = 0f;
	private Transform _t;
	
	// Use this for initialization
	void Start () 
	{
		_t = transform; //Caches our transform
		_startYposition = _t.position.y; //Stores the original Y value
	}
	
	// Update is called once per frame
	void Update () 
	{
		//Destroy the enemy if he moves outside the visible area
		if(_t.position.x > 8.0f || _t.position.x < -8.0f)
		{
			Destroy(gameObject);
		}
		
		//Moving the enemy in a zig zag
		_t.position = new Vector3(_t.position.x, _startYposition + Mathf.PingPong(Time.time * zigZagSpeed, zigZagDistance), 0f);
		
		//Moving our enemy
		_t.Translate(Time.deltaTime * movementSpeed, 0f, 0f);
	}
}
